Search some literals strings in a string¶
Search some literals strings in a string.
Sample text : ‘The quick brown fox jumps over the lazy dog.’
Searched words : ‘fox’, ‘dog’, ‘horse’
import re
patterns = [
'fox',
'dog',
'horse'
]
S = 'The quick brown fox jumps over the lazy dog.'
for pattern in patterns:
print('Searching for "%s" in "%s" ->' % (pattern, text),)
if re.search(pattern, S):
print('Matched!')
else:
print('Not Matched!')
Output:
Searching for "fox" in "The quick brown fox jumps over the lazy dog."
Matched!
Searching for "dog" in "The quick brown fox jumps over the lazy dog."
Matched!
Searching for "horse" in "The quick brown fox jumps over the lazy dog."
Not Matched!